import pygtk
pygtk.require('2.0')
import gtk
import os

from mokocard.util.sqlutil import SqlUtil
from mokocard.util.ioutil import importFile
from mokocard.gui.modaldialog import ModalDialog
from mokocard.obj.configdata import ConfigData

class ConfigPage(gtk.VBox):
        
    def createDatabase(self, widget):
        
        pathDatabase = self.entryPathDb.get_text()
        
        if pathDatabase == None or pathDatabase == "":
            modal = ModalDialog("No database path")
            return None
        elif os.path.exists(pathDatabase) == 1:
            # file/directory already exists
            modal = ModalDialog("File already exists")
            return None
        
        ret = self.sqlUtil.createDatabase(pathDatabase)

        if ret == False:
            modal = ModalDialog("Database not created")
        else:
            modal = ModalDialog("Database created")
                
    def loadImportFile(self, widget):
        # no connecton no import
        if self.sqlUtil.connection == None:
            modal = ModalDialog("No database open")
            return None
        elif self.sqlUtil.isReadOnly() == True:
            modal = ModalDialog("Database Read Only")
            return None
       
        newcards = importFile(self.entryPathImportFile.get_text(),self.entrySeparator.get_text())
        
        if newcards == None:
            modal = ModalDialog("No file imported")
            return None
        
        okCard = 0;
        koCard = 0;
        
        for card in newcards:
            ret = self.sqlUtil.insertCard(card[0], card[1])
            if ret:
                okCard = okCard + 1
            else:
                koCard = koCard + 1
            
        modal = ModalDialog(str(okCard) + "/" + str(koCard+okCard) + " Imported")
        
    def setMainPage(self, mainPage):
        self.mainPage = mainPage;
        
    def loadDatabase(self, widget):
        
        pathDatabase = self.entryPathDb.get_text()
        if pathDatabase == None or pathDatabase == "":
            modal = ModalDialog("No database path")
            return None
        
        # all cards option only in read only mode
        if self.checkAllCards.get_active() == True and self.checkReadOnly.get_active() == False:
            modal = ModalDialog("All card in read only mode!") 
            return None
        
        ret = self.sqlUtil.openDatabase(pathDatabase, self.checkReverse.get_active(), self.checkReadOnly.get_active(), self.checkAllCards.get_active())
        
        if ret == False:
            modal = ModalDialog("Database not loaded")
            return None
        
        # reset counter
        self.mainPage.counter = -1
        # disable ok/ko button
        self.mainPage.disableAllNoStart()
        
        modal = ModalDialog("Database Loaded")
        
    def build(self, configData):
        
        ########## begin select database ########## 
        # hButtonBox for select database
        
        # build table
        tableOpenDatabase = gtk.Table(2,1,False)
        tableOpenDatabase.set_border_width(10)
        tableOpenDatabase.show()
        
        labelOpenDatabase = gtk.Label("Database")
        labelOpenDatabase.show()
              
        # parameter homogeneous, spacing
        hOpenDatabaseBox = gtk.HBox(False,10)
        hOpenDatabaseBox.show()
        
        # text path db
        # using self i can manage it in entire class
        # we use gtk.Entry instead of gtk.TextView 
        self.entryPathDb = gtk.Entry()
        self.entryPathDb.set_text(configData.getDefaultDatabase())
        self.entryPathDb.show()
        
        btCreateDatabase = gtk.Button("  Create  ")
        btCreateDatabase.connect("clicked", self.createDatabase)
        btCreateDatabase.set_size_request(configData.getButtonConfigWidth(), configData.getButtonConfigHeight())
        btCreateDatabase.show()
        
        btLoadDatabase = gtk.Button("  Load  ")
        btLoadDatabase.set_size_request(configData.getButtonConfigWidth(), configData.getButtonConfigHeight())
        btLoadDatabase.connect("clicked", self.loadDatabase)
        btLoadDatabase.show()
        
        hOpenDatabaseBox.pack_start(self.entryPathDb, True, True, 10)
        hOpenDatabaseBox.pack_start(btCreateDatabase, False, False, 10)
        hOpenDatabaseBox.pack_start(btLoadDatabase, False, False, 10)
        
        ########## end select database ##########
        
        ########## begin database option ##########
        
        # reverse check button
        self.checkReverse = gtk.CheckButton(label="Reverse")
        self.checkReverse.show()
        # read only check button
        self.checkReadOnly = gtk.CheckButton(label="Read Only")
        self.checkReadOnly.show()
        # load all cards (read only mode)
        self.checkAllCards = gtk.CheckButton(label="All")
        self.checkAllCards.show()
        
        hDatabaseOptionBox = gtk.HBox(False,10)
        hDatabaseOptionBox.show()
        
        hDatabaseOptionBox.pack_start(self.checkReverse, False, False, 10)
        hDatabaseOptionBox.pack_start(self.checkReadOnly, False, False, 10)
        hDatabaseOptionBox.pack_start(self.checkAllCards, False, False, 10)
        
        tableOpenDatabase.attach(labelOpenDatabase,0,1,0,1, ypadding=10)
        tableOpenDatabase.attach(hOpenDatabaseBox,0,1,1,2, ypadding=10)
        tableOpenDatabase.attach(hDatabaseOptionBox,0,1,2,3, ypadding=10)

        ########## end database option ##########
         
        ########## begin import file ##########
        
        # build table
        tableImportFile= gtk.Table(2,1,False)
        tableImportFile.set_border_width(10)
        tableImportFile.show()
                
        # hButtonBox for import file
        hImportFileBox = gtk.HBox(False,10)
        hImportFileBox.show()

        # we use gtk.Entry instead of gtk.TextView 
        self.entryPathImportFile = gtk.Entry()
        self.entryPathImportFile.set_text(configData.getDefaultImportFile())
        self.entryPathImportFile.show()
               
        btLoadImportFile = gtk.Button("  Load  ")
        btLoadImportFile.connect("clicked", self.loadImportFile)
        btLoadImportFile.set_size_request(configData.getButtonConfigWidth(), configData.getButtonConfigHeight())
        btLoadImportFile.show()
        
        # parameters child, expand, fill, padding
        hImportFileBox.pack_start(self.entryPathImportFile, True, True, 10)
        hImportFileBox.pack_start(btLoadImportFile, False, False, 10)
        
        labelImportFile = gtk.Label("Import file")
        labelImportFile.show()

        tableImportFile.attach(labelImportFile,0,1,0,1, ypadding=10)
        tableImportFile.attach(hImportFileBox,0,1,1,2, ypadding=10)
        
        ########## end import file ##########
        
        ########## begin separator ##########
        
        hConfig1 = gtk.HBox(False,10)
        hConfig1.show()
        
        lblSeparator = gtk.Label("Cvs separator:")
        lblSeparator.show()
        
        self.entrySeparator = gtk.Entry()
        self.entrySeparator.set_text(configData.getSeparator())
        self.entrySeparator.show()
        
        hConfig1.pack_start(lblSeparator, False, False, 10)
        hConfig1.pack_start(self.entrySeparator, True, True, 10)
        
        ########## end separator ##########
       
        # parameters child, expand, fill, padding
        self.pack_start(tableOpenDatabase, False, False, 10)
        self.pack_start(tableImportFile, False, False, 10)
        self.pack_start(hConfig1, False, False, 10)
        
    def __init__(self, configData, sqlUtil):

        self.sqlUtil = sqlUtil
        
        # ten pixels space
        gtk.VBox.__init__(self, False, 10)
       
        self.build(configData)
        self.show()